﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;

public partial class Pages_Public_ShowAdd : BasePage
{
    private int _ID = 0;
    
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            CheckAdvertisement();
        }
    }
    protected void BindPageInfo()
    {
        _ID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        Page.Title = WebUtil.GetPageTitle("Show Advertisement");
    }
    protected void CheckAdvertisement()
    {
        if (_ID > 0)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            var advertisement = context.Advertisements.FirstOrDefault(P => P.ID == _ID);
            if (advertisement == null)
                ShowNotFoundMessage();
            else
            {
                if (advertisement.LinkedUrl.IsNullOrEmpty())
                    ShowNotFoundMessage();
                else
                {
                    ///Update the Hit Counter
                    //UpdateHitCounter();
                    advertisement.HitCounter++;
                    context.SaveChanges();
                    String url = WebUtil.GetCompleteUrl(advertisement.LinkedUrl);
                    Response.Redirect(url);
                }
            }
        }
        else
            ShowNotFoundMessage();
    }
    protected void ShowNotFoundMessage()
    {
        WebUtil.ShowMessageBox(divMessage, "Sorry! Requested Advertisement was not found.", true);
    }

    //protected void UpdateHitCounter() {
    //    Advertisement entity = null;
    //    BtcHousingEntities context = new BtcHousingEntities();
        
    //    if (_ID > 0)
    //        entity = context.Advertisements.FirstOrDefault(P => P.ID == _ID); //dao.GetByID(_ID);
    //    else
    //        entity = new Advertisement();

    //    entity.HitCounter = entity.HitCounter + 1;
    //    context.SaveChanges();
    //}
}